home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / programming / c / bitmap24 / zbitmap24.h < prev   
Encoding:
C/C++ Source or Header  |  1999-05-17  |  3.6 KB  |  154 lines

  1. #ifndef ZBITMAP24_H
  2. #define ZBITMAP24_H
  3. /* -------------------------------------------------------------------------- *\
  4.    ZBITMAP24.H, a BitMap24 subclass which introduces simple Z-Buffering, header file
  5.    Copyright (C) 1999  Jarno van der Linden
  6.    jarno@kcbbs.gen.nz
  7.  
  8.    This library is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU Library General Public
  10.    License as published by the Free Software Foundation; either
  11.    version 2 of the License, or (at your option) any later version.
  12.  
  13.    This library is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    Library General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU Library General Public
  19.    License along with this library; if not, write to the
  20.    Free Software Foundation, Inc., 59 Temple Place - Suite 330, Cambridge,
  21.    MA 02139, USA.
  22.  
  23.  
  24.    01 May 1999: Brought it all together in some sort of distributable form
  25. \* -------------------------------------------------------------------------- */
  26.  
  27. /* -------------------------------- Includes -------------------------------- */
  28. #include <exec/types.h>
  29.  
  30. #include "bitmap24.h"
  31.  
  32. /* ------------------------------ Definitions ------------------------------- */
  33. enum {
  34.     ZBITMAP24_ERROR_NOZBUFFER = 100
  35. };
  36.  
  37. /* --------------------------------- Macros --------------------------------- */
  38.  
  39. /* -------------------------------- Typedefs -------------------------------- */
  40.  
  41. /* ------------------------------ Proto Types ------------------------------- */
  42.  
  43. /* -------------------------------- Structs --------------------------------- */
  44. class ZBitMap24 : public BitMap24
  45. {
  46.     public:
  47.         ZBitMap24();
  48.         ~ZBitMap24();
  49.  
  50.         char *GetErrorStr(int error);
  51.         char *GetErrorStr();
  52.  
  53.         void SetMaxZ(double maxz);
  54.  
  55.         inline UWORD GetBufferZ(int x,int y);
  56.         inline UWORD GetBufferZ(double z);
  57.  
  58.         void SetColour(UBYTE r,UBYTE g,UBYTE b,double z,int x,int y);
  59.         void SetColour(const Colour &c, double z,int x,int y);
  60.  
  61.         virtual inline void SetColourFast(UBYTE r,UBYTE g,UBYTE b,double z,int x,int y);
  62.         virtual inline void SetColourFast(const Colour &c,double z, int x,int y);
  63.  
  64.         inline BOOL CanDraw(int x,int y,double z);
  65.         inline BOOL CanDrawSet(int x,int y,double z);
  66.  
  67.         virtual inline UWORD *GetZBuffer();
  68.  
  69.         virtual void SetSize(UWORD width,UWORD height);
  70.  
  71.     private:
  72.         UWORD *zbuffer;
  73.         double mul,maxz;
  74. };
  75.  
  76.  
  77. /* -------------------------------- Globals --------------------------------- */
  78.  
  79. /* ---------------------------------- Code ---------------------------------- */
  80. inline UWORD ZBitMap24::GetBufferZ(int x,int y)
  81. {
  82.     return zbuffer[y*GetWidthFast()+x];
  83. }
  84.  
  85.  
  86. inline UWORD ZBitMap24::GetBufferZ(double z)
  87. {
  88.     return (UWORD)(mul * log(z+1.0));
  89. }
  90.  
  91.  
  92. inline void ZBitMap24::SetColourFast(UBYTE r,UBYTE g,UBYTE b,double z,int x,int y)
  93. {
  94.     UWORD zb,*zbp;
  95.  
  96.     zb = GetBufferZ(z);
  97.     zbp = &(zbuffer[y*GetWidthFast()+x]);
  98.  
  99.     if(zb <= *zbp)
  100.     {
  101.         *zbp = zb;
  102.         BitMap24::SetColourFast(r,g,b,x,y);
  103.     }
  104. }
  105.  
  106.  
  107. inline void ZBitMap24::SetColourFast(const Colour &c,double z,int x,int y)
  108. {
  109.     UWORD zb,*zbp;
  110.  
  111.     zb = GetBufferZ(z);
  112.     zbp = &(zbuffer[y*GetWidthFast()+x]);
  113.  
  114.     if(zb <= *zbp)
  115.     {
  116.         *zbp = zb;
  117.         BitMap24::SetColourFast(c,x,y);
  118.     }
  119. }
  120.  
  121.  
  122. inline BOOL ZBitMap24::CanDraw(int x,int y,double z)
  123. {
  124.     if(GetBufferZ(z) <= GetBufferZ(x,y))
  125.         return TRUE;
  126.  
  127.     return FALSE;
  128. }
  129.  
  130.  
  131. inline BOOL ZBitMap24::CanDrawSet(int x,int y,double z)
  132. {
  133.     UWORD zb, *zbp;
  134.  
  135.     zb = GetBufferZ(z);
  136.     zbp = &(zbuffer[y*GetWidthFast()+x]);
  137.  
  138.     if(zb <= *zbp)
  139.     {
  140.         *zbp = zb;
  141.         return TRUE;
  142.     }
  143.  
  144.     return FALSE;
  145. }
  146.  
  147.  
  148. inline UWORD *ZBitMap24::GetZBuffer()
  149. {
  150.     return zbuffer;
  151. }
  152.  
  153. #endif /* ZBITMAP24_H */
  154.